package IO;

import java.io.*;
import java.util.Scanner;

public class Demo12 {
    public static void main(String[] args) throws FileNotFoundException {
        //1.输入必要的信息
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的文件路径: ");
        String srcPath = scanner.next();
        System.out.println("请输入要复制到的目标路劲为: ");
        String destPath = scanner.next();

        //2.验证上述输入的路径是否合法
        File srcFile = new File(srcPath);
        if(! srcFile.isFile()){
            System.out.println("输入的文件路径非法!");
            return;
        }
        File destFile = new File(destPath);
        if (! destFile.getParentFile().isDirectory()){        //getParentFile这个方法返回当前文件的父路径,即文件名前面的路径
            System.out.println("输入要复制到的目标路径非法!");
            return;
        }

        //3.读写文件
        try(InputStream inputStream = new FileInputStream(srcFile);
        OutputStream outputSteam = new FileOutputStream(destFile)){
            while(true) {
            byte[] bytes = new byte[1024];
            int n = inputStream.read(bytes);
            if(n == -1){
                break;
            }
            outputSteam.write(bytes,0,n);
        }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
